package br.academic.zumeh.persistence;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.hibernate.Session;
import org.hibernate.ejb.EntityManagerImpl;

public abstract class GenericDAOImpl<T> implements GenericDAO<T>{
	
	
	
	private static final long serialVersionUID = 2995238964976424481L;
	
	@PersistenceContext(unitName = "NewZumeh")
	protected EntityManager entityManager;
	

	@Override
	public void removeObjeto(Long objId) {
		T obj = localizaObjeto(objId);
		
		if (obj != null) {
			entityManager.remove(obj);
		}
	}

	@Override
	public void insereObjeto(T obj) {

		entityManager.merge(obj);

	}
	
	protected Session getSession() {
        Session session;
        if (entityManager.getDelegate() instanceof EntityManagerImpl) {
                EntityManagerImpl entityManagerImpl = (EntityManagerImpl) entityManager
                                .getDelegate();
                session = entityManagerImpl.getSession();
        } else {
                session = (Session) entityManager.getDelegate();
        }
        return session;
}

	public abstract T localizaObjeto(Long objId);

	public abstract List<T> listaObjetos();

	public abstract void deletaObjetos();


}
